# import nltk
# nltk.download('averaged_perceptron_tagger_eng')
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords

from nltk import pos_tag
import matplotlib.pyplot as plt

input_str = ' Today\' s weather is good, very windy and sunny, \
we have no classes in the afternoon, We have to play basketball tomorrow \
  good good'

# 分词
tokens = word_tokenize(input_str)
tokens = [s.lower() for s in tokens]
# 过滤停顿词
stopwords_set = stopwords.words('english')
tokens = [w for w in tokens if w not in stopwords_set]

# 抽出句子的主题成分，进行解析
tags = pos_tag(tokens)
print(tags)

input_str = "the little yellow dog died"
tokens = word_tokenize(input_str)
tokens = [s.lower() for s in tokens]
tags = pos_tag(tokens)
print(tags)
"""
{<DT>?<JJ>*<NN>}：正则表达式模式，用于匹配名词短语的结构：
<DT>?：可选的限定词（如 the, a, an）
<JJ>*：零个或多个形容词（如 little, yellow）
<NN>：必须包含的名词（如 dog）
"""
grammer = 'MY_NP: {<DT>?<JJ>*<NN>}'
cp = nltk.RegexpParser(grammer)
result = cp.parse(tags)
print(result)
result.draw()
plt.show()
